package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import tp.pr5.Direction;
import tp.pr5.NavigationObserver;
import tp.pr5.PlaceInfo;


public class NavigationPanel extends JPanel implements NavigationObserver
{

	private static final long serialVersionUID = 1L;
	
	private JPanel cityMap;
	private JScrollPane log;
	private JPanel image;
	
	private JTextArea textArea;
	private JLabel label;

	private PlaceCell[][] placeCell;
	private int fil = 5;
	private int col = 5;
	
	/**
	 * Main Constructor
	 */
	
	public NavigationPanel()
	{
		super();
		this.placeCell = new PlaceCell[11][11];
		this.cityMap = new JPanel();
		this.image = new JPanel();
		this.label = new JLabel();
		this.textArea = new JTextArea(6, 80);
		this.log = new JScrollPane(textArea);
		build();
	}
	
	/**
	 * Builds the user interface
	 */
	
	public void build()
	{

		//Creates the 11x11 navigation panel
		for (int y = 0; y < 11; y++)
        {
            for (int x = 0; x < 11; x++)
            {
            	this.placeCell[x][y] = new PlaceCell(this);
            	cityMap.add(this.placeCell[x][y]);				  
            }
        }
		this.textArea.setEditable(false);
		
		this.setLayout(new BorderLayout());
		this.add(cityMap, BorderLayout.CENTER);
		this.add(log, BorderLayout.SOUTH);
		this.add(image, BorderLayout.WEST);
		
		cityMap.setBorder(BorderFactory.createTitledBorder("City Map"));
		cityMap.setLayout(new GridLayout(11, 11));
		
		log.setBorder(BorderFactory.createTitledBorder("Log"));
		
		image.setLayout(new BorderLayout());
		image.add(label, BorderLayout.CENTER);
		
		this.label.setIcon(new ImageIcon(ImageIcon.class.getResource("/imagenes/walleNorth.png")));

	}

	/**
	 * Changes the image depending on the robots direction
	 * 
	 * @param direction of the robot
	 */
	@Override
	public void headingChanged(Direction newHeading) 
	{
		String dir = null;
		
		switch (newHeading)
		{
			case EAST: dir = "walleEast.png"; break;
			case NORTH: dir = "walleNorth.png"; break;
			case SOUTH: dir = "walleSouth.png"; break;
			case WEST: dir = "walleWest.png"; break;
			case UNKNOWN: break;
			default: break;
		}
		this.label.setIcon(new ImageIcon(ImageIcon.class.getResource("/imagenes/" + dir)));	
	}

	/**
	 * Notifies that the navigation module has been initialized
	 * 
	 * @param initialPlace - The place where the robot starts the simulation
	 * @param heading - The initial robot heading
	 */
	@Override
	public void initNavigationModule(PlaceInfo initialPlace, Direction heading)
	{
		PlaceCell pc = this.placeCell[fil][col]; 
		pc.setPlaceInfo(initialPlace);
		pc.arriveAt(initialPlace);
		pc.setText(initialPlace.getName());
		this.textArea.setText(initialPlace.toString());
		this.headingChanged(heading);
		this.textArea.setText(initialPlace.toString());
	}

	/**
	 * Notifies that the robot has arrived at a place
	 * 
	 * @param heading - The robot movement direction
	 * @param place - The place where the robot arrives
	 */
	@Override
	public void robotArrivesAtPlace(Direction heading, PlaceInfo place)
	{
		this.placeCell[fil][col].left();
		
		switch (heading)
		{
			case EAST: this.fil++; break;
			case NORTH: this.col--; break;
			case SOUTH: this.col++; break;
			case WEST: this.fil--; break;
			case UNKNOWN: break;
			default: break;
		}
		PlaceCell pc = this.placeCell[fil][col]; 
		pc.setPlaceInfo(place);
		pc.arriveAt(place);
		pc.setText(place.getName());
		this.textArea.setText(place.toString());
	}

	/**
	 * Notifies that the user requested a RADAR instruction
	 * 
	 * @param placeDescription - Information with the current place
	 */
	@Override
	public void placeScanned(PlaceInfo placeDescription)
	{
		this.textArea.setText(placeDescription.toString());
	}

	/**
	 * Notifies that the place where the robot stays has changed (because the robot picked or dropped an item)
	 * 
	 * @param placeDescription - Information with the current place
	 */
	@Override
	public void placeHasChanged(PlaceInfo placeDescription) 
	{
		this.textArea.setText(placeDescription.toString());
	}

}
